package com.ktjy.springredis.service;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * @return 13141
 * 2025/7/23
 * 14:58
 */
@Service("redisService")
public class RedisService {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisService.class);
    @Resource
    private RedisTemplate redisTemplate;

    // 封装简单插入操作
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            LOGGER.error("set error: key {}, value {}", key, value, e);
        }
        return result;
    }

    // 封装简单获取操作
    public Object get(final String key) {
        Object result = null;
        try {
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            result = operations.get(key);
        } catch (Exception e) {
            LOGGER.error("get error: key {}", key, e);
        }
        return result;
    }

    // 封装删除操作
    public boolean delete(final String key) {
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            LOGGER.error("delete error: key {}", key, e);
        }
        return result;
    }

    // 判断key是否存在
    public boolean exists(final String key) {
        boolean exists = false;
        try {
            exists = redisTemplate.hasKey(key);
        } catch (Exception e) {
            LOGGER.error("exists error: key {}", key, e);
        }
        return exists;
    }

    // 更新操作：如果key存在则更新，如果不存在则插入
    public boolean update(final String key, Object value) {
        boolean result = false;
        try {
            // 判断key是否存在
            if (exists(key)) {
                // 直接进行覆盖（实际上set方法本身就是覆盖原值）
                ValueOperations<String, Object> operations = redisTemplate.opsForValue();
                operations.set(key, value);
                result = true;
            } else {
                LOGGER.warn("Update failed, key {} does not exist.", key);
            }
        } catch (Exception e) {
            LOGGER.error("update error: key {}, value {}", key, value, e);
        }
        return result;
    }

    // 封装设置过期时间操作
    public boolean setWithExpiration(final String key, Object value, long timeout) {
        boolean result = false;
        try {
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value, timeout);
            result = true;
        } catch (Exception e) {
            LOGGER.error("setWithExpiration error: key {}, value {}, timeout {}", key, value, timeout, e);
        }
        return result;
    }
}
